<!DOCTYPE html>
<html lang="en">
<head>
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <script type="text/javascript" src="../static/lib/html5shiv.js"></script>
    <script type="text/javascript" src="../static/lib/respond.min.js"></script>
    <script type="text/javascript" src="../static/lib/jquery/1.9.1/jquery.min.js"></script>
    <link rel="stylesheet" href="../static/h-ui/css/H-ui.css" />
    <script type="text/javascript" src="../static/h-ui/js/H-ui.js"></script>
    <link rel="stylesheet" href="../static/css/company.css" />
    <meta charset="UTF-8">
    <title>My Stores</title>
</head>
<body>
<div class="add-brand">
    <input class="btn btn-secondary radius" type="button" onClick="storeadd()" value="新增店铺">
</div>
<div class="panel2">
    <div class="panel-header1">Amazon</div>
    <div>
        <!-- Table -->
        <table id="table1" class="table table-border table-bordered table-hover" style="border-radius: 5px;">
            <thead>
            <tr>
                <td width="40px" style="text-align: center;font-weight: bold">编号</td>
                <td style="text-align: center;font-weight: bold">店铺ID</td>
                <td style="text-align: center;font-weight: bold">店铺名称</td>
                <td style="text-align: center;font-weight: bold">Market Type</td>
                <td style="text-align: center;font-weight: bold">Marketplace ID</td>
                <td style="text-align: center;font-weight: bold">MWS Auth Token</td>
                <td width="120px" style="text-align: center;font-weight: bold">操作</td>
            </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
    </div>

</div>
<div class="panel2">
    <div class="panel-header1">eBay</div>
    <div>
        <!-- Table -->
        <table id="table2" class="table table-border table-bordered table-hover" style="border-radius: 5px;">
            <thead>
            <tr>

                <td width="40px" style="text-align: center;font-weight: bold">编号</td>
                <td style="text-align: center;font-weight: bold">店铺ID</td>
                <td style="text-align: center;font-weight: bold">店铺名称</td>
                <td style="text-align: center;font-weight: bold">Market Type</td>
                <td style="text-align: center;font-weight: bold">Marketplace ID</td>
                <td style="text-align: center;font-weight: bold">MWS Auth Token</td>
                <td width="120px" style="text-align: center;font-weight: bold">操作</td>
            </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
    </div>
</div>
<div id="store-add" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content radius">
            <div class="modal-header">
                <h3 class="modal-title">Store Add</h3>
                <a class="close" data-dismiss="modal" aria-hidden="true" href="javascript:void(0);">×</a>
            </div>
            <div class="modal-body">
                <form action="" method="post" class="form form-horizontal" id="form-store-add">
                    <div class="row cl">
                        <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>店铺ID：</label>
                        <div class="formControls col-xs-8 col-sm-9">
                            <input type="text" class="input-text" value="" placeholder="" id="storeid" name="storeid">
                        </div>

                    </div>
                    <div class="row cl">
                        <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>店铺名称：</label>
                        <div class="formControls col-xs-8 col-sm-9">
                            <input type="text" class="input-text" value="" placeholder="" id="storename" name="storename">
                        </div>
                    </div>
                    <div class="row cl">
                        <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>网点类型：</label>
                        <div class="mt-20 skin-minimal">
                            <div class="radio-box">
                                <input type="radio" id="amazon" name="type" value="amazon">Amazon
                            </div>
                            <div class="radio-box">
                                <input style="margin-left: 300px" type="radio" id="ebay" name="type" value="ebay" checked="checked">eBay
                            </div>
                        </div>
                    </div>
                    <div class="row cl">
                        <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>Marketplace ID：</label>
                        <div class="formControls col-xs-8 col-sm-9">
                            <input type="text" class="input-text" value="" placeholder="Marketplace ID：" id="mkid" name="mkid">
                        </div>
                    </div>
                    <div class="row cl">
                        <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>MWS Auth Token：</label>
                        <div class="formControls col-xs-8 col-sm-9">
                            <input type="text" class="input-text" value="" placeholder="MWS Auth Token：" id="token3" name="token3">
                        </div>
                    </div>
                    <div class="row cl">
                        <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                            <input class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
                        </div>
                    </div>
                </form>

            </div>
        </div>
    </div>
</div>
<div id="store-edit" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content radius">
            <div class="modal-header">
                <h3 class="modal-title">Store Edit</h3>
                <a class="close" data-dismiss="modal" aria-hidden="true" href="javascript:void(0);">×</a>
            </div>
            <div class="modal-body">
                <form action="" method="post" class="form form-horizontal" id="form-store-edit">
                    <div class="row cl">
                        <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>店铺ID：</label>
                        <div class="formControls col-xs-8 col-sm-9">
                            <input type="text" class="input-text" value="" placeholder="" id="storeid1" name="storeid">
                        </div>

                    </div>
                    <div class="row cl">
                        <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>店铺名称：</label>
                        <div class="formControls col-xs-8 col-sm-9">
                            <input type="text" class="input-text" value="" placeholder="" id="storename1" name="storename">
                        </div>
                    </div>
                    <div class="row cl">
                        <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>网点类型：</label>
                        <div class="mt-20 skin-minimal">
                            <div class="radio-box">
                                <input type="radio" name="type1" value="amazon" checked disabled>Amazon
                            </div>
                            <div class="radio-box">
                                <input type="radio" name="type1" value="ebay" disabled>eBay
                            </div>
                        </div>
                    </div>
                    <div class="row cl">
                        <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>Marketplace ID：</label>
                        <div class="formControls col-xs-8 col-sm-9">
                            <input type="text" class="input-text" value="" placeholder="Marketplace ID：" id="mkid1" name="mkid" disabled>
                        </div>
                    </div>
                    <div class="row cl">
                        <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>MWS Auth Token：</label>
                        <div class="formControls col-xs-8 col-sm-9">
                            <input type="text" class="input-text" value="" placeholder="MWS Auth Token：" id="token1" name="token" disabled>
                        </div>
                    </div>
                    <div class="row cl">
                        <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                            <input class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<!--_footer 作为公共模版分离出去-->
<script type="text/javascript" src="../static/lib/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="../static/lib/layer/2.4/layer.js"></script>
<script type="text/javascript" src="../static/h-ui/js/H-ui.min.js"></script>
<script type="text/javascript" src="../static/h-ui.admin/js/H-ui.admin.js"></script> <!--/_footer 作为公共模版分离出去-->

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="../static/lib/My97DatePicker/4.8/WdatePicker.js"></script>
<script type="text/javascript" src="../static/lib/jquery.validation/1.14.0/jquery.validate.js"></script>
<script type="text/javascript" src="../static/lib/jquery.validation/1.14.0/validate-methods.js"></script>
<script type="text/javascript" src="../static/lib/jquery.validation/1.14.0/messages_zh.js"></script>

<script type="text/javascript">

    window.onload = getstores;
    var amazons = []
    var ebays = []
    var store = {}
    var bvoId = [[${bvoId}]]
    var pkStore = 0;

    function tableadd1() {
        var table1 = document.getElementById('table1');
        console.log("here:tableadd-table1");
        console.log(amazons.length);
        for (var i = 1; i <= amazons.length; i++) {
            console.log("here test table1");
            console.log(amazons[i - 1]);
            table1.innerHTML += "<tr>\n" +

                "<td id=\"pkStore\" style=\"text-align: center;\">" + amazons[i - 1].pkStore + "</td>\n" +
                "<td id=\"storeId\" style=\"text-align: center;\">" + amazons[i - 1].storeId + "</td>\n" +
                "<td id=\"storeName\" style=\"text-align: center;\">" + amazons[i - 1].storeName + "</td>\n" +
                "<td id=\"marketType\" style=\"text-align: center;\">" + amazons[i - 1].marketType + "</td>\n" +
                "<td id=\"marketplaceId\" style=\"text-align: center;\">" + amazons[i - 1].marketplaceId + "</td>\n" +
                "<td id=\"mwsToken\" style=\"text-align: center;\">" + amazons[i - 1].mwsToken + "</td>\n" +
                //
                "<td style=\"text-align: center;\"><input id=\"btnamazonedit\" class=\"btn btn-primary-outline radius\" type=\"button\" value=\"编辑\" onclick='btnamazonedit("+i+")'>\n" +
                "<input id=\"btnstoredel\" class=\"btn btn-danger-outline radius\" type=\"button\" value=\"删除\" onclick='btnamazondel("+i+")'></td>\n" +
                "</tr>";
        }
    }
    function tableadd2() {
        var table2 = document.getElementById('table2');
        console.log("here:tableadd-table2");
        for(var j = 1; j<=ebays.length; j++){
            console.log(ebays[j-1]);
            table2.innerHTML += "<tr>\n" +

                "<td id=\"storeId\" style=\"text-align: center;\">"+ebays[j-1].pkStore+"</td>\n" +
                "<td id=\"storeId\" style=\"text-align: center;\">"+ebays[j-1].storeId+"</td>\n" +
                "<td id=\"storeName\" style=\"text-align: center;\">"+ebays[j-1].storeName+"</td>\n" +
                "<td id=\"marketType\" style=\"text-align: center;\">"+ebays[j-1].marketType+"</td>\n" +
                "<td id=\"marketplaceId\" style=\"text-align: center;\">"+ebays[j-1].marketplaceId+"</td>\n" +
                "<td id=\"mwsToken\" style=\"text-align: center;\">"+ebays[j-1].mwsToken+"</td>\n" +
                //
                "<td style=\"text-align: center;\"><input id=\"btnebayedit\" class=\"btn btn-primary-outline radius\" type=\"button\" value=\"编辑\" onclick='btnebayedit("+j+")'>\n" +
                "<input id=\"btnstoredel2\" class=\"btn btn-danger-outline radius\" type=\"button\" value=\"删除\" onclick='btnebaydel("+j+")'></td>\n"+
                "</tr>";
        }
    }

    function getstores(){
        $.ajax({
            type:'POST',
            url:'/store/getstoretype',
            contentType:"application/json",
            data:JSON.stringify({"bvoId":bvoId,"marketType":"amazon"}),
            dataType:'json',
            success:function (data) {
                console.log(data.data)
                amazons = data.data;
                tableadd1();
            }
        });
        $.ajax({
            type:'POST',
            url:'/store/getstoretype',
            contentType:"application/json",
            data:JSON.stringify({"bvoId":bvoId,"marketType":"ebay"}),
            dataType:'json',
            success:function (data) {
                console.log(data.data)
                ebays = data.data;
                tableadd2();
            }
        });
    }

    //获取对话框页面店铺信息
    function getedit() {
        var storeId = $("#storeid1").val();
        var storeName = $("#storename1").val();
        var marketType = $("input[name='type1']:checked").val();
        var marketplaceId = $("#mkid1").val();
        var mwsToken = $("#token1").val();
        var editInfo = {
            'pkStore': pkStore,
            'storeId': storeId,
            'storeName': storeName,
            'marketType': marketType,
            'marketplaceId': marketplaceId,
            'mwsToken': mwsToken,
            'bvoId': bvoId
        }
        console.log("update store: ",editInfo)
        return editInfo;
    }

    //获取新增店铺信息
    function getadd(){
        var storeId = $("#storeid").val();
        var storeName = $("#storename").val();
        var marketType = $("input[name='type']:checked").val();
        var marketplaceId = $("#mkid").val();
        var mwsToken = $("#token3").val();
        var addInfo = {
            'storeId': storeId,
            'storeName': storeName,
            'marketType': marketType,
            'marketplaceId': marketplaceId,
            'mwsToken': mwsToken,
            'bvoId': bvoId
        }
        console.log("add store: ",addInfo)
        return addInfo;
    }

    //获取表格table1中数据
    function btnamazonedit(i){
        var table1 = document.getElementById("table1");
        var child = table1.getElementsByTagName("tr")[i];
        var cols = child.getElementsByTagName("td");
        var pkstore = cols[0].innerHTML;
        var storeId = cols[1].innerHTML;
        var storeName = cols[2].innerHTML;
        var marketType = cols[3].innerHTML;
        var marketplaceId = cols[4].innerHTML;
        var mwsToken = cols[5].innerHTML;
        store = {
            'pkStore':pkstore,
            'storeId':storeId,
            'storeName':storeName,
            'marketType':marketType,
            'marketplaceId':marketplaceId,
            'mwsToken':mwsToken
        }
        pkStore = pkstore;
        $("#store-edit").modal("show");
        $("#storeid1").attr("value",storeId);
        $("#storename1").attr("value",storeName);
        $("#mkid1").attr("value",marketplaceId);
        $("#token1").attr("value",mwsToken);
        // var type = document.getElementsByName("type1");
        $("input[name='type1'][value='amazon']").attr("checked",true);
        $(":radio[name=type1][value='amazon']").attr('checked','checked');

        console.log("store",store);

    }

    //获取表格table2中数据
    function btnebayedit(j){
        var table2 = document.getElementById("table2");
        var child = table2.getElementsByTagName("tr")[j];
        var cols = child.getElementsByTagName("td");
        var pkstore = cols[0].innerHTML;
        var storeId = cols[1].innerHTML;
        var storeName = cols[2].innerHTML;
        var marketType = cols[3].innerHTML;
        var marketplaceId = cols[4].innerHTML;
        var mwsToken = cols[5].innerHTML;
        $("#store-edit").modal("show");
        store = {
            'pkStore':pkstore,
            'storeId':storeId,
            'storeName':storeName,
            'marketType':marketType,
            'marketplaceId':marketplaceId,
            'mwsToken':mwsToken
        }
        pkStore = pkstore;
        console.log(cols[1].innerHTML);
    }

    // function editstore(type,i){
    //     if(type===("amazon")){
    //         // btnamazonedit(i);
    //         var table1 = document.getElementById("table1");
    //         var child = table1.getElementsByTagName("tr")[i];
    //         var cols = child.getElementsByTagName("td");
    //         var pkstore = cols[1].innerHTML;
    //         var storeId = cols[2].innerHTML;
    //         var storeName = cols[3].innerHTML;
    //         var marketType = cols[4].innerHTML;
    //         var marketplaceId = cols[5].innerHTML;
    //         var mwsToken = cols[6].innerHTML;
    //         store = {
    //             'pkStore':pkstore,
    //             'storeId':storeId,
    //             'storeName':storeName,
    //             'marketType':marketType,
    //             'marketplaceId':marketplaceId,
    //             'mwsToken':mwsToken
    //         }
    //         pkStore = pkstore;
    //         $("#storeid1").attr("value",storeId);
    //         $("#storename1").attr("value",storeName);
    //         $("#mkid1").attr("value",marketplaceId);
    //         $("#token1").attr("value",mwsToken);
    //         // var type = document.getElementsByName("type1");
    //         $("input[name='type1'][value='amazon']").attr("checked",true);
    //     }else{
    //         // btnebayedit(i);
    //         var table2 = document.getElementById("table2");
    //         var child2 = table2.getElementsByTagName("tr")[i];
    //         var cols2 = child2.getElementsByTagName("td");
    //         var pkstore2 = cols2[1].innerHTML;
    //         var storeId2 = cols2[2].innerHTML;
    //         var storeName2 = cols2[3].innerHTML;
    //         var marketType2 = cols2[4].innerHTML;
    //         var marketplaceId2 = cols2[5].innerHTML;
    //         var mwsToken2 = cols2[6].innerHTML;
    //         store = {
    //             'pkStore':pkstore2,
    //             'storeId':storeId2,
    //             'storeName':storeName2,
    //             'marketType':marketType2,
    //             'marketplaceId':marketplaceId2,
    //             'mwsToken':mwsToken
    //         }
    //         pkStore = pkstore;
    //         $("#storeid1").attr("value",storeId2);
    //         $("#storename1").attr("value",storeName2);
    //         $("#mkid1").attr("value",marketplaceId2);
    //         $("#token1").attr("value",mwsToken2);
    //         // var type = document.getElementsByName("type1");
    //         $("input[name='type1'][value='ebay']").attr("checked",true);
    //     }
    //
    //     $("#store-edit").modal("show");
    // }

    function storeadd(){
        $("#store-add").modal("show");
    }


    $(function(){
        $('.skin-minimal input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        });
        $("#form-store-add").validate({
            rules:{
                storeid:{
                    required:true,
                    minlength:1,
                    maxlength:50
                },storename:{
                    required:true,
                    minlength:1,
                    maxlength:50
                },type:{
                    required:true,
                },mkip:{
                    required:true,
                    minlength:1,
                    maxlength:50
                },token:{
                    maxlength:100
                },

            },
            onkeyup:false,
            focusCleanup:true,
            success:"valid",
            submitHandler:function(form){
                var msg = getadd();
                console.log("add post",msg)
                $.ajax({
                    type:'POST',
                    url:"/store/addst",
                    data:JSON.stringify(msg),
                    contentType:"application/json",
                    dataType:'json',
                    success:function () {
                        console.log("新增信息成功")
                        window.location.href = "/store/gobvostore";
                    }
                });
            }
        });
    });

    $(function(){
        $('.skin-minimal input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        });
        $("#form-store-edit").validate({
            rules:{
                storeid1:{
                    required:true,
                    minlength:1,
                    maxlength:50
                },storename1:{
                    required:true,
                    minlength:1,
                    maxlength:50
                }
            },
            onkeyup:false,
            focusCleanup:true,
            success:"valid",
            submitHandler:function(form){
                var msg = getedit();
                console.log("edit post",msg)
                $.ajax({
                    type:'POST',
                    url:"/store/editst",
                    data:JSON.stringify(msg),
                    contentType:"application/json",
                    dataType:'json',
                    success:function () {
                        console.log("修改信息成功")
                        window.location.href = "/store/gobvostore";
                    }
                });
            }
        });
    });

    function btnamazondel(i){
        var table1 = document.getElementById("table1");
        var child = table1.getElementsByTagName("tr")[i];
        var cols = child.getElementsByTagName("td");
        var pkstore = cols[0].innerHTML;
        pkStore = pkstore;
        console.log("pkStore",pkStore);
        store = {
            'pkStore':pkStore,
            'state':'0'
        }
        $.ajax({
            type:'POST',
            url:'/store/delst',
            contentType:"application/json",
            data:JSON.stringify(store),
            dataType:'json',
            success:function (data) {
                console.log(data.data)
                window.location.href="/store/gobvostore";
            }
        });
    }
    function btnebaydel(j){
        var table2 = document.getElementById("table2");
        var child = table2.getElementsByTagName("tr")[j];
        var cols = child.getElementsByTagName("td");
        var pkstore = cols[0].innerHTML;
        pkStore = pkstore;
        console.log("pkStore",pkStore);
        store = {
            'pkStore':pkStore,
            'state':'0',
        }
        $.ajax({
            type:'POST',
            url:'/store/delst',
            contentType:"application/json",
            data:JSON.stringify(store),
            dataType:'json',
            success:function (data) {
                console.log(data.data)
                window.location.href="/store/gobvostore";
            }
        });
    }


</script>
</body>
</html>